<?php

/**
 * controler para movimientos de ingreso stock
 *
 * @author hparra
 */
require_once 'class/im/transfer/DP/ImTransferDP.class.php';
require_once 'class/im/transfer/DP/ImTransferDetDP.class.php';
require_once 'class/im/transfer/FN/ImTransferFN.class.php';
require_once 'class/common/mc/FN/XxMaestroListaFN.class.php';
require_once 'class/im/alm/FN/ImAlmacenFN.class.php';

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/DP/ImStockMovimDP.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/DP/ImStockDP.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/FN/ImStockFN.class.php');

class ImTransferController extends ControllerBase
{
    public function listar()
    {
		if (!isset($vars)){
			$vars = '';
		}        
        $this->view->show("im/transfer/listar",$vars);
    }

    public function contenedor()
    {
        $o_ImTransferFN= new ImTransferFN();       
        
        $page = isset($this->requestVars['page']) ? $this->requestVars['page'] : 1;
        $rp = isset($this->requestVars['rp']) ? $this->requestVars['rp'] : 10;
        $sortname = isset($this->requestVars['sortname']) ? $this->requestVars['sortname'] : 'name';
        $sortorder = isset($this->requestVars['sortorder']) ? $this->requestVars['sortorder'] : 'desc';		
        $o_ImTransferFN->page = $page;
        $o_ImTransferFN->tamPag = $rp;
        $o_ImTransferFN->sortCampos = $sortname;
        $o_ImTransferFN->sortType   = $sortorder;
        if ($this->requestVars){
            if (array_key_exists('s_TransferId', $this->requestVars)) {
                $o_ImTransferFN->setTransferId($this->requestVars["s_TransferId"]) ;   
            }
            if (array_key_exists('s_DocTransfer', $this->requestVars)) {
                $o_ImTransferFN->setDocTransfer($this->requestVars["s_DocTransfer"]);  
            }      
            if (array_key_exists('s_DocRef', $this->requestVars)) {
                $o_ImTransferFN->setDocRef($this->requestVars["s_DocRef"]);  
            }                     
        }
        $result = $o_ImTransferFN->select();     
        $vars['result'] = $result;
        $vars['o_ImTransferFN'] = $o_ImTransferFN;
        if (array_key_exists('modoBusqueda', $this->requestVars)) {
            $vars['modoBusqueda'] = $this->requestVars["modoBusqueda"];
        }else{
            $vars['modoBusqueda'] = "";
        }
        $this->view->show("im/transfer/contenedor",$vars);

    }
    
     public function nuevo()
     {
		$o_XxXxMaestroListaFN = new XxMaestroListaFN();
        
        $o_XxXxMaestroListaFN->setListaId('transfTipo');
        $a_TipoTransf = $o_XxXxMaestroListaFN->select();
        
        $o_XxXxMaestroListaFN->setListaId('transfEsta');
        $a_TransEsta = $o_XxXxMaestroListaFN->select();
        
        $o_ImAlmacenFN  = new ImAlmacenFN();
        $ao_ImAlmacenBN = $o_ImAlmacenFN->select();
         
        $vars['ao_ImAlmacenBN']             = $ao_ImAlmacenBN;        
        $vars['a_TipoTransf']              = $a_TipoTransf;
        $vars['a_TransEsta']              = $a_TransEsta;
     
        $this->view->show("im/transfer/nuevo",$vars);
    }
    
    
    public function guardarSave()
    {
      try 
      {
        if ($this->requestVars){
            $o_ImTransferDP                = new ImTransferDP();
            
            $o_ImTransferDP->setDocTransfer($this->requestVars["s_DocTransfer"]);
            $o_ImTransferDP->setTipoTransfer($this->requestVars["s_TipoTransfer"]);
            $o_ImTransferDP->setEstado($this->requestVars["s_Estado"]);
            $o_ImTransferDP->setDocRef($this->requestVars["s_DocRef"]);
            $o_ImTransferDP->setAlmDestinoId($this->requestVars["s_AlmDestinoId"]);
            $o_ImTransferDP->setAlmOrigenId($this->requestVars["s_AlmOrigenId"]);
            $o_ImTransferDP->setObs($this->requestVars["obs"]); 
            $o_ImTransferDP->setCreaFecha(date('Y-m-d H:m:s')); 
                                  
            $o_ImTransferDP->save();
            
            if($this->requestVars["a_ItemId"] )
            {
                $i=1;
                foreach ($this->requestVars["a_ItemId"] as $iKey => $value)
                {
                    $o_ImTransferDetDP = new ImTransferDetDP();
                    
                    $o_ImTransferDetDP->setTransferId($o_ImTransferDP->getTransferId());	      
                    $o_ImTransferDetDP->setTransferDetId($i);               
                    $o_ImTransferDetDP->setArticuloId($this->requestVars["a_ItemId"][$iKey]);                        
                    $o_ImTransferDetDP->setCant($this->requestVars["a_Cant"][$iKey]);
                    $o_ImTransferDetDP->setCreaFecha(date('Y-m-d H:m:s'));
                    $o_ImTransferDetDP->save();
                    
                    $i++;
                }
            }
            $a_result['transferId'] = $o_ImTransferDP->getTransferId();
            $a_result['rpta'] = 'true';
            $a_result['msg'] = '';
              
          }
      }catch (Exception $e)
      {
        $a_result['rpta'] = 'false';
        $a_result['msg'] = $e->getMessage().' en '.$e->getFile().' linea '.$e->getLine();        
      }
      
      echo json_encode($a_result);
    
    }
    
    
    public function editar()
    {
         if ($this->requestVars)
         {
            $o_ImTransferFN= new ImTransferFN();
            if (array_key_exists('s_TransferId', $this->requestVars)) {
                $o_ImTransferFN->setTransferId($this->requestVars["s_TransferId"]) ;   
            }
           

              
            $result = $o_ImTransferFN->getImTransfer(); 
        
    		$o_XxXxMaestroListaFN = new XxMaestroListaFN();
            
            $o_XxXxMaestroListaFN->setListaId('transfTipo');
            $a_TipoTransf = $o_XxXxMaestroListaFN->select();
            
            $o_XxXxMaestroListaFN->setListaId('transfEsta');
            $a_TransEsta = $o_XxXxMaestroListaFN->select();
            
            $o_ImAlmacenFN  = new ImAlmacenFN();
            $ao_ImAlmacenBN = $o_ImAlmacenFN->select();           
        }
       
        
        
         
        $vars['ao_ImAlmacenBN']              = $ao_ImAlmacenBN;        
        $vars['a_TipoTransf']                = $a_TipoTransf;
        $vars['a_TransEsta']                 = $a_TransEsta;
        $vars['o_ImTransferBN']              = $result;
     
        $this->view->show("im/transfer/editar",$vars);
    }
    
    public function editarSave()
    {
      try 
      {
        if ($this->requestVars){
            $o_ImTransferDP                = new ImTransferDP();
            
            $o_ImTransferDP->setTransferId($this->requestVars["s_TransferId"]);
            $o_ImTransferDP->setDocTransfer($this->requestVars["s_DocTransfer"]);
            $o_ImTransferDP->setTipoTransfer($this->requestVars["s_TipoTransfer"]);
            $o_ImTransferDP->setEstado($this->requestVars["s_Estado"]);
            $o_ImTransferDP->setDocRef($this->requestVars["s_DocRef"]);
            $o_ImTransferDP->setAlmDestinoId($this->requestVars["s_AlmDestinoId"]);
            $o_ImTransferDP->setAlmOrigenId($this->requestVars["s_AlmOrigenId"]);
            $o_ImTransferDP->setObs($this->requestVars["obs"]); 
            $o_ImTransferDP->setCreaFecha(date('Y-m-d H:m:s')); 
                                  
            $o_ImTransferDP->update();
            
            if($this->requestVars["a_ItemId"] )
            {
                $o_ImTransferDetDP = new ImTransferDetDP();
                $o_ImTransferDetDP->setTransferId($o_ImTransferDP->getTransferId());
                $o_ImTransferDetDP->eliminarImTransferDetDP();
                $i=1;
                foreach ($this->requestVars["a_ItemId"] as $iKey => $value)
                {
                    $o_ImTransferDetDP = new ImTransferDetDP();
                    
                    $o_ImTransferDetDP->setTransferId($o_ImTransferDP->getTransferId());	      
                    $o_ImTransferDetDP->setTransferDetId($i);               
                    $o_ImTransferDetDP->setArticuloId($this->requestVars["a_ItemId"][$iKey]);                        
                    $o_ImTransferDetDP->setCant($this->requestVars["a_Cant"][$iKey]);
                    $o_ImTransferDetDP->setCreaFecha(date('Y-m-d H:m:s'));
                    $o_ImTransferDetDP->save();
                    
                    $i++;
                }
            }
            $a_result['transferId'] = $o_ImTransferDP->getTransferId();
            $a_result['rpta'] = 'true';
            $a_result['msg'] = "";
              
          }
      }catch (Exception $e)
      {
        $a_result['rpta'] = 'false';
        $a_result['msg'] = $e->getMessage().' en '.$e->getFile().' linea '.$e->getLine();        
      }
      
      echo json_encode($a_result);
    
    }

    public function cerrar(){
       if ($this->requestVars)
       {   
          if (array_key_exists('s_TransferId', $this->requestVars)) 
          {     
            $o_ImTransferFN= new ImTransferFN();
            if (array_key_exists('s_TransferId', $this->requestVars)) {
                $o_ImTransferFN->setTransferId($this->requestVars["s_TransferId"]) ; 
                $o_ImTransferBN = $o_ImTransferFN->getImTransfer();
            }                        
          }
       }
       $a_detalleTransfer = $o_ImTransferBN->getObj_ImTransferDet();
       foreach ($a_detalleTransfer as $iKey => $value)
       {
           $o_ImStockOrigFN = new ImStockFN();
           $o_ImStockOrigFN->setAlmId($o_ImTransferBN->getAlmOrigenId());
           $o_ImStockOrigFN->setArtId($value->getArticuloId());
           $o_ImStockOrigBN = $o_ImStockOrigFN->selectByPK();           
           if ($o_ImStockOrigBN){
               if ($o_ImStockOrigBN->getCantFisico() < $value->getCant()){
                    $swErrorStock = 1;
                    $swErrorStockArticulo = $value->getArticuloId();
                    $swErrorStockStock    = $o_ImStockOrigBN->getCantFisico();
                    $swErrorStockCant     = $value->getCant();
               }
           }           
       }            
       if (!$swErrorStock){
            reset($a_detalleTransfer);       
            foreach ($a_detalleTransfer as $iKey => $value)
            { 
                $o_ImStockOrigFN = new ImStockFN();
                $o_ImStockOrigFN->setAlmId($o_ImTransferBN->getAlmOrigenId());
                $o_ImStockOrigFN->setArtId($value->getArticuloId());
                $o_ImStockOrigBN = $o_ImStockOrigFN->selectByPK();           
                if ($o_ImStockOrigBN){
                    $costoAlmOrig  = $o_ImStockOrigBN->getCostoProm();
                }
                $o_ImStockDestFN = new ImStockFN();
                $o_ImStockDestFN->setAlmId($o_ImTransferBN->getAlmDestinoId());
                $o_ImStockDestFN->setArtId($value->getArticuloId());
                $o_ImStockDestBN = $o_ImStockDestFN->selectByPK();           

                //Salida desde orig.
                $o_ImStockMovimDP  = new ImStockMovimDP();
                $o_ImStockMovimDP->setAlmId($o_ImTransferBN->getAlmOrigenId());
                $o_ImStockMovimDP->setArtId($value->getArticuloId());
                $o_ImStockMovimDP->setCant(($value->getCant()*(-1)));
                if ($o_ImStockOrigBN){
                     $o_ImStockMovimDP->setCantAnt($o_ImStockOrigBN->getCantFisico());     
                }

                $o_ImStockMovimDP->setCosto($costoAlmOrig);
                $o_ImStockMovimDP->setCreaFecha(date("Y-m-d H:i:s"));
                $o_ImStockMovimDP->setCreaUsr($value->getCreaUsr());
                $o_ImStockMovimDP->setDocId($this->requestVars["s_TransferId"]);
                $o_ImStockMovimDP->setDocNum($o_ImTransferBN->getDocTransfer);
                $o_ImStockMovimDP->setDocReferenNro($o_ImTransferBN->getDocRef());
                $o_ImStockMovimDP->setIngSal("S");
                $o_ImStockMovimDP->setMovFecha(date("Y-m-d"));
                $o_ImStockMovimDP->setMovTipo("T");
                $o_ImStockMovimDP->save();

                $o_ImStockDP = new ImStockDP();
                $o_ImStockDP->setAlmId($o_ImTransferBN->getAlmOrigenId()) ;   
                $o_ImStockDP->setArtId($value->getArticuloId());
                $o_ImStockDP->setCostoUlt($costoAlmOrig);

                if ($o_ImStockOrigBN){
                    $a = $o_ImStockOrigBN->getCantFisico()-$value->getCant();
                    $o_ImStockDP->setCantFisico($a);   
                    $o_ImStockDP->update();               
                }

                //Ingreso hacia dest.
                $o_ImStockMovimDP  = new ImStockMovimDP();
                $o_ImStockMovimDP->setAlmId($o_ImTransferBN->getAlmDestinoId());
                $o_ImStockMovimDP->setArtId($value->getArticuloId());
                $o_ImStockMovimDP->setCant($value->getCant());
                if ($o_ImStockDestBN){
                    $o_ImStockMovimDP->setCantAnt($o_ImStockDestBN->getCantFisico());
                }
                $o_ImStockMovimDP->setCosto($costoAlmOrig);
                $o_ImStockMovimDP->setCreaFecha(date("Y-m-d H:i:s"));
                $o_ImStockMovimDP->setCreaUsr($value->getCreaUsr());
                $o_ImStockMovimDP->setDocId($this->requestVars["s_TransferId"]);
                $o_ImStockMovimDP->setDocNum($o_ImTransferBN->getDocTransfer);
                $o_ImStockMovimDP->setDocReferenNro($o_ImTransferBN->getDocRef());
                $o_ImStockMovimDP->setIngSal("I");
                $o_ImStockMovimDP->setMovFecha(date("Y-m-d"));
                $o_ImStockMovimDP->setMovTipo("T");
                $o_ImStockMovimDP->save();

                $o_ImStockDP = new ImStockDP();
                $o_ImStockDP->setAlmId($o_ImTransferBN->getAlmDestinoId()) ;   
                $o_ImStockDP->setArtId($value->getArticuloId());
                $o_ImStockDP->setCostoUlt($costoAlmOrig);

                if ($o_ImStockDestBN){
                    $o_ImStockDP->setCostoProm(($o_ImStockDestBN->getCostoProm()*$o_ImStockDestBN->getCantFisico()) + ($costoAlmOrig*$value->getCant())/($value->getCant()+$o_ImStockDestBN->getCantFisico()));                    
                    $o_ImStockDP->setCantFisico($o_ImStockDestBN->getCantFisico() + $value->getCant());
                    $o_ImStockDP->update();
                }else{
                    $o_ImStockDP->setCantFisico($value->getCant());
                    $o_ImStockDP->setCostoProm($costoAlmOrig);               
                    $o_ImStockDP->save();                              
                }

                $result = $o_ImStockDestFN->select();
            }

            $o_ImTransferDP  = new ImTransferDP();
            $o_ImTransferDP->setTransferId($this->requestVars["s_TransferId"]);
            $o_ImTransferDP->setEstado("C");       
            $o_ImTransferDP->update();
            $vars['result'] = $result;
            $vars['stockFN'] = $o_ImStockFN;
            $a_result['s_TransferId'] = $this->requestVars["s_TransferId"];
            $a_result['rpta'] = 'true';
       }else{
           $a_result['msg'] = 'Stock insuficiente para el articulo '.$swErrorStockArticulo ."(".$swErrorStockStock."<".$swErrorStockCant.")";
           $a_result['rpta'] = 'false';
       }
       echo json_encode($a_result);
    }    
    
}